/*
Copyright (C) 2013 Bengt Martensson.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/.
*/

package org.harctoolbox.guicomponents;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map.Entry;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.harctoolbox.harchardware.beacon.AmxBeaconListener;
import org.harctoolbox.harchardware.beacon.AmxBeaconListener.Node;

public class AmxBeaconListenerPanel extends HarcPanel {

    private static final String helpText =
            "This pane displays a dynamic tree of nodes sending out an AMX beacon.";

    public static final String versionString = "0.1.0";
    public static final String aboutText =
            "AmxBeaconListener version " + versionString + ".\n"
            + "Copyright 2013 by Bengt Martensson.\n\n"
            + "License: GPL3.\n\n"
            + "Project home page: http://www.harctoolbox.org";
    private static final long serialVersionUID = 1L;


    private final transient AmxBeaconListener listener;

    @Override
    public String getHelpMessage() {
        return helpText;
    }

    @Override
    public String getAboutMessage() {
        return aboutText;
    }

    @Override
    public String getProgName() {
        return "AmxBeaconListener";
    }

    @Override
    public String getIconPath() {
        return "/icons/Crystal-Clear/22x22/actions/search.png";
    }

    @Override
    public void close(){
        listener.stop();
    }

    private class Callbacker implements AmxBeaconListener.Callback {

        Callbacker() {
        }

        @Override
        public void func(HashMap<InetAddress, Node> hm) {
            DefaultMutableTreeNode root = new DefaultMutableTreeNode("Nodes");
            for (Node n : hm.values()) {
                DefaultMutableTreeNode node = doNode(n);
                root.add(node);
            }
            DefaultTreeModel treeModel = new DefaultTreeModel(root);
            tree.setModel(treeModel);
        }

        private DefaultMutableTreeNode doNode(Node node) {
            DefaultMutableTreeNode n = new DefaultMutableTreeNode(node.getInetAddress().getHostName());
            for (Entry<String,String>kvp : node.getTable().entrySet()) {
                DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(kvp.getKey() + ": " + kvp.getValue());
                n.add(leaf);
            }
            return n;
        }
    }

    /**
     * Creates new form AmxBeaconListenerPanel
     */
    public AmxBeaconListenerPanel() {
        listener = new AmxBeaconListener(new Callbacker());
        listener.start();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Nodes");
        tree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        scrollPane.setViewportView(tree);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 225, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables
}
